package com.course.service.impl;

import com.course.entity.ResultBody;
import com.course.mapper.FeedBackMapper;
import com.course.service.FeedBackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class FeedBackServiceImpl implements FeedBackService {
    @Autowired
    private FeedBackMapper feedBackMapper;
//    @Override
//    public ResultBody feedback(int courseid,String usernumber,Double coursepoint,String question) {
//        if(feedBackMapper.feedback(courseid,usernumber,coursepoint,question)!=0){
//            return new ResultBody(200,"反馈成功",true);
//        }
//        return new ResultBody(400,"反馈失败",false);
//    }

    @Override
    public ResultBody lookfeedback(Integer courseid) {
        if (feedBackMapper.lookfeedback(courseid) != null) {
            return new ResultBody(200, "查看反馈信息成功", feedBackMapper.lookfeedback(courseid), feedBackMapper.lookfeedback(courseid).size());
        }
        return new ResultBody(400, "查看反馈信息失败", false);
    }

    @Override
    public ResultBody lookfeedbackpoint(Map<String, Object> map) {
        if (feedBackMapper.lookfeedbackpoint(map) != null || feedBackMapper.lookfeedbackpoint(map) == null) {
            return new ResultBody(200, "查看反馈信息成功", feedBackMapper.lookfeedbackpoint(map), feedBackMapper.lookfeedbackpoint(map).size());
        } else
            return new ResultBody(400, "查看反馈信息失败", false);
    }

    @Override
    public ResultBody getFeedbackData() {
        List<Map<String, Object>> rawData = feedBackMapper.getFormattedFeedback();

        if (rawData.isEmpty()) {
            return new ResultBody(404, "未查询到相关信息", false);
        }

        // 按课程ID分组
        Map<Integer, Map<String, Object>> courseMap = new LinkedHashMap<>();

        // 收集所有问题和选项的完整映射
        Map<String, Set<String>> allQuestionOptions = new LinkedHashMap<>();

        // 第一次遍历：收集所有问题和选项
        for (Map<String, Object> row : rawData) {
            String title = (String) row.get("title");
            String option = (String) row.get("selection_answer");

            if (option != null) {
                allQuestionOptions.putIfAbsent(title, new LinkedHashSet<>());
                allQuestionOptions.get(title).add(option);
            }
        }

        // 第二次遍历：构建课程数据结构
        for (Map<String, Object> row : rawData) {
            Integer courseId = ((Number) row.get("courseid")).intValue(); // 安全转换
            String courseName = (String) row.get("name");
            String teacher = (String) row.get("teacher");

            // 初始化课程结构
            courseMap.putIfAbsent(courseId, new LinkedHashMap<>());
            Map<String, Object> courseData = courseMap.get(courseId);
            courseData.put("name", courseName);
            courseData.put("teacher", teacher);

            // 初始化questions数组
            if (!courseData.containsKey("questions")) {
                List<Map<String, Object>> questions = new ArrayList<>();

                // 为每个问题创建结构，包含所有可能的选项
                for (Map.Entry<String, Set<String>> entry : allQuestionOptions.entrySet()) {
                    Map<String, Object> question = new LinkedHashMap<>();
                    question.put("title", entry.getKey());

                    List<Map<String, Object>> data = new ArrayList<>();
                    for (String option : entry.getValue()) {
                        data.add(new LinkedHashMap<String, Object>() {{
                            put("name", option);
                            put("value", 0); // 初始化为0
                        }});
                    }

                    question.put("data", data);
                    questions.add(question);
                }

                courseData.put("questions", questions);
            }
        }

        // 第三次遍历：填充实际统计值
        for (Map<String, Object> row : rawData) {
            Integer courseId = ((Number) row.get("courseid")).intValue(); // 安全转换
            String title = (String) row.get("title");
            String option = (String) row.get("selection_answer");
            Number count = (Number) row.get("count"); // 保持为Number类型

            if (option != null && count != null && courseMap.containsKey(courseId)) {
                List<Map<String, Object>> questions = (List<Map<String, Object>>) courseMap.get(courseId).get("questions");

                for (Map<String, Object> question : questions) {
                    if (title.equals(question.get("title"))) {
                        List<Map<String, Object>> data = (List<Map<String, Object>>) question.get("data");

                        for (Map<String, Object> optionData : data) {
                            if (option.equals(optionData.get("name"))) {
                                // 使用Number的intValue()方法安全转换
                                optionData.put("value", count.intValue());
                                break;
                            }
                        }
                        break;
                    }
                }
            }
        }

        // 构建最终结果
        List<Map<String, Object>> resultList = new ArrayList<>(courseMap.values());

        return new ResultBody(200, "获取反馈成功", resultList, resultList.size());
    }

    @Override
    public ResultBody lookallfeedbackpoint() {
        if (feedBackMapper.lookallfeedbackpoint() != null || feedBackMapper.lookallfeedbackpoint() == null) {
            return new ResultBody(200, "查看反馈信息成功", feedBackMapper.lookallfeedbackpoint(), feedBackMapper.lookallfeedbackpoint().size());
        } else
            return new ResultBody(400, "查看反馈信息失败", false);
    }
}
//    @Override
//    public ResultBody displayfeedback()
//    {
//        if(feedBackMapper.displayfeedback()!=null||feedBackMapper.displayfeedback()==null)
//        {
//            return new ResultBody(200,"查看反馈信息成功",feedBackMapper.displayfeedback(),feedBackMapper.displayfeedback().size());
//        }
//        else
//            return new ResultBody(400,"查看反馈信息失败",false);
//    }
//    @Override
//    public ResultBody lookfeedbackname()
//    {
//        if(feedBackMapper.lookfeedbackname()!=null||feedBackMapper.lookfeedbackname()==null)
//        {
//            return new ResultBody(200,"查看反馈信息成功",feedBackMapper.lookfeedbackname(),feedBackMapper.lookfeedbackname().size());
//        }
//        else
//            return new ResultBody(400,"查看反馈信息失败",false);
//    }


